package com.wdzl.controller;

import com.wdzl.pojo.Percent;
import com.wdzl.service.PercentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("percent")
public class PercentController {
    private final PercentService percentService;

    public PercentController(PercentService percentService) {
        this.percentService = percentService;
    }

    @RequestMapping("list")
    public ModelAndView star(){
        List<Percent> select = percentService.select ();
        ModelAndView modelAndView=new ModelAndView ();
        modelAndView.setViewName ("star");
        modelAndView.addObject ("list",select);
        return modelAndView;
    }
    @RequestMapping("xiu/{id}")
    public String xiu(@PathVariable("id") String id, Model model){
        Percent selectone = percentService.selectone (Integer.parseInt (id));
        model.addAttribute ("select",selectone);
return "s";
    }
    @GetMapping("add")
    public String add(){
        return "add";
    }
    @PostMapping("add")
    public String a(Percent percent){
        percentService.insert (percent);
        return "redirect:/percent/list";
    }
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id") String id){
        int i=Integer.parseInt (id);
        percentService.delete (i);
        return "redirect:/percent/list";
    }
    @GetMapping("gai/{id}")
    public String gai(@PathVariable("id") String id,Model model){
        int i=Integer.parseInt (id);
        Percent selectone = percentService.selectone (i);
        model.addAttribute ("mess",selectone);
        return "gai";
    }
    @PostMapping("gai")
    public String ga(Percent percent,Model model){
       percentService.up (percent);
        return "redirect:/percent/list";
    }
    @GetMapping("counts")
    public String count(){
        return "count";
    }
    @RequestMapping("count")
    public String cou(String sid,String aid,String money){
        float v = Float.parseFloat (money);

        int zhuan = percentService.zhuan (sid, aid, v);
        if (zhuan!=2){throw new RuntimeException ("转账异常");}

        return "redirect:/percent/list";
    }
//    @ExceptionHandler()
//    public String run(Exception e, ModelMap modelMap){
//        modelMap.addAttribute ("because","改变失败，请确定你输入的id号是否正确，或其他问题.");
//        modelMap.addAttribute ("e",e);
//        return "erroy";
//    }

}
